package fem3d;

import inf.jlinalg.SolveFailedException;
import inf.text.ArrayFormat;

import java.io.IOException;

import org.junit.Test;

import fem2.Block;
import fem2.Constraint;
import fem2.Debugger;
import fem2.Element;
import fem2.Force;
import fem2.MaterialModel;
import fem2.Mesh;
import fem2.MeshGenerator;
import fem2.MeshUtilities;
import fem2.Model;
import fem2.Node;
import fem2.analysis.Analysis;
import fem2.analysis.LinearStaticAnalysis;
import fem2.element.StructuralElement;
import fem2.enu.EchoLevelType;
import fem2.enu.State;
import fem2.enu.StressType;
import fem2.material.StVenantKirchhoffMM;
import fem2.pre_and_post.GidMeshGenerator;
import fem2.pre_and_post.GidPostStructural;

/**
 * desmonstrate 8-node hexahedra capability with St-Venant Kirchhoff material
 * 
 * @author hbui
 * 
 */
public class Beam3dBig {

	static String projDir = "/home/hbui/kratos_janosch";
	static String projName = "beam3d-topopt.gid";
	String fn = projDir + '/' + projName + '/' + "mesh2.msh";

	public Model createModel() {
		MeshGenerator mg = new GidMeshGenerator(fn);
		Mesh mesh = mg.getMesh(3);

		Model m = new Model(mesh);

		mesh.setNumberOfFields(3);

		/*
		 * element creation
		 */
		double mu = 1;
		double nu = 0.3;
		double E = 2 * mu * (1 + nu);
		double t = 1.0;
		State ss = State.THREE_DIMENSIONAL;
		MaterialModel mm = new StVenantKirchhoffMM(E, nu, t, 0, ss);
		for (int i = 0; i < mesh.countBlocks(); i++) {
			StructuralElement e = new StructuralElement(mesh.getBlock(i), mm);
			m.addElement(e);
		}

		/*
		 * conditions
		 */
		Constraint c1 = new Constraint(false, false, false);
		mesh.setConstraint(c1, MeshUtilities.seekNodesOnSurface(mesh, 1, 0, 0, 0));

		Node n = MeshUtilities.seekNode(mesh, 60, 10, 10);
		double F = -1;
		Force f = new Force(n, 0, F, 0);
		m.addLoad(f);

		return m;
	}

	@Test
	public void printGausspoint() {
		double l = 1;
		double w = 1;
		double h = 1;
		Mesh mesh = new Mesh();
		Node n1 = mesh.addNode(new Node(0, 0, 0));
		Node n2 = mesh.addNode(new Node(l, 0, 0));
		Node n3 = mesh.addNode(new Node(l, w, 0));
		Node n4 = mesh.addNode(new Node(0, w, 0));
		Node n5 = mesh.addNode(new Node(0, 0, h));
		Node n6 = mesh.addNode(new Node(l, 0, h));
		Node n7 = mesh.addNode(new Node(l, w, h));
		Node n8 = mesh.addNode(new Node(0, w, h));
		Element e = new StructuralElement(new Block(n1, n2, n3, n4, n5, n6, n7, n8), null);
		int ng = e.getNumIntegrationPoints();
		for (int i = 0; i < ng; i++) {
			Debugger.watch(i + ":", e.getIntegrationPoint(i));
		}
	}

	public static void main(String[] args) throws SolveFailedException, IOException {
		Model m = new Beam3dBig().createModel();

		Mesh mesh = m.getMesh();

		/*
		 * analysis
		 */
		// Solver solver = new BaitschSolver();
		// mesh.setNumberOfFields(3);
		// M.preAnalysis();
		// M.Initialize();
		// int n = mesh.countDofs();
		// solver.setSize(n);
		// M.assembleKt(solver.getA());
		//
		// double[] r = new double[n];
		// r = M.assembleR(r);
		// double[] u = r.clone();
		// solver.solve(u);
		// M.setX(1, u);
		// M.FinalizeNonlinearIteration();

		/*
		 * analysis
		 */
		Analysis an = new LinearStaticAnalysis(m);
		// ((NewtonRaphsonBasedStrategy) an.getStrategy())
		// .setMaxNewtonRaphsonIteration(1);
		an.getStrategy().setEchoLevel(EchoLevelType.OUTPUT);
		// an.getStrategy().addObserver(new StressObserver(m, null));
		an.run();

		/*
		 * post process
		 */
		for (int i = 0; i < mesh.countNodes(); i++) {
			System.out.println(ArrayFormat.format(mesh.getNode(i).getUHatLocal()));
		}

		GidPostStructural post = new GidPostStructural(projDir, projName);
		post.clearAllPostFiles();
		post.writeMesh(m, 0);
		post.writeNodalDisplacementResults(m, 0);
		post.writeGaussPointStressResults(m, 0, StressType.CAUCHY_STRESS);

		Debugger.watch("Analysis completed");
	}
}
